// 屏幕宽高
const winW = document.body.clientWidth
const winH = document.body.clientHeight
// 分页对象
const page = {
  pageNum: 1,
  pageSize: 20,
  total: 0,
  list: []
}

/**
 * 分页
 */
const setPage = (_fun) => {
  kkpager.generPageHtml({
    pno: page.pageNum,
    total: Math.ceil(page.total / page.pageSize), // 总页码
    totalRecords: page.total, // 总数据条数
    mode: 'click', // 默认值是link，可选link或者click
    click(n) {
      this.selectPage(n)
      page.pageNum = n
      _fun()
      return false
    }
  })
}

/**
 * 生成并返回一个从m到n全区间的随机数
 * @param {Object} m
 * @param {Object} n
 */
const randomNum = (m, n) => {
  return Math.floor(Math.random() * (n - m + 1) + m)
}

/**
 * 渲染星空
 */
const showSky = () => {
  let htmlStr = ''
  for (let i = 0; i < 200; i++) {
    // 随机位置
    const x = randomNum(0, winW)
    const y = randomNum(0, winH)
    const second = 0.5 + Number(Math.random().toFixed(1))
    htmlStr = `${htmlStr }<div class="star" style="animation: star ${ second }s linear infinite; left:${ x }px;top:${ y }px"><img src="@/images/star.png" /></div>`
  }
  $('#starrySky').append(htmlStr)
}

/**
 * 菜单切换
 * @param {Object} _type 1场景，2人物，3道具，4其他
 */
const changeMenu = (_type) => {
  const url = `@/views/index.html?pageNum=1&type=${ _type}`
  window.location.href = url
}

/**
 * 获取对象格式的URL参数
 */
const getUrlParm = () => {
  const parameter = {}
  if (window.location.search) {
    const urlParams = window.location.search.substring(1).split('&')
    for (let i = 0; i < urlParams.length; i++) {
      const item = urlParams[i]
      if (item.indexOf('=') !== -1) {
        const params = urlParams[i].split('=')
        parameter[params[0]] = params[1]
      }
    }
  }
  return parameter
}
